package com.hhq.sso.common.api;

import java.io.Serializable;

import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Getter;
import lombok.Setter;

/**
 * <p>
 * Description: some description
 * </p>
 *
 * @author : xiaodong.yang
 * @date : 2024/6/17 15:02
 */
@Schema(description = "返回信息")
@Setter
@Getter
public class CommonResult<T> implements Serializable {

    @Schema(description = "状态码")
    private int code;
    @Schema(description = "是否成功")
    private boolean success;
    @Schema(description = "承载数据")
    private T data;
    @Schema(description = "返回消息")
    private String msg;

    private CommonResult(ResultCode resultCode) {
        this(resultCode, null, resultCode.getMessage());
    }

    private CommonResult(ResultCode resultCode, String msg) {
        this(resultCode, null, msg);
    }

    private CommonResult(ResultCode resultCode, T data) {
        this(resultCode, data, resultCode.getMessage());
    }

    private CommonResult(ResultCode resultCode, T data, String msg) {
        this(resultCode.getCode(), data, msg);
    }

    private CommonResult(int code, T data, String msg) {
        this.code = code;
        this.data = data;
        this.msg = msg;
        this.success = ResultCode.SUCCESS.code == code;
    }

    public static boolean isSuccess(CommonResult<?> result) {
        return ResultCode.SUCCESS.code == result.getCode();
    }

    public static boolean isNotSuccess(CommonResult<?> result) {
        return !isSuccess(result);
    }

    public static <T> CommonResult<T> data(T data) {
        return data(data, "操作成功");
    }

    public static <T> CommonResult<T> data(T data, String msg) {
        return data(200, data, msg);
    }

    public static <T> CommonResult<T> data(int code, T data, String msg) {
        return new CommonResult<>(code, data, data == null ? "暂无承载数据" : msg);
    }

    public static <T> CommonResult<T> success() {
        return new CommonResult<>(ResultCode.SUCCESS);
    }

    public static <T> CommonResult<T> success(String msg) {
        return new CommonResult<>(ResultCode.SUCCESS, msg);
    }

    public static <T> CommonResult<T> success(ResultCode resultCode) {
        return new CommonResult<>(resultCode);
    }

    public static <T> CommonResult<T> success(ResultCode resultCode, String msg) {
        return new CommonResult<>(resultCode, msg);
    }

    public static <T> CommonResult<T> fail(String msg) {
        return new CommonResult<>(ResultCode.FAILURE, msg);
    }

    public static <T> CommonResult<T> fail(int code, String msg) {
        return new CommonResult<>(code, null, msg);
    }

    public static <T> CommonResult<T> fail(ResultCode resultCode) {
        return new CommonResult<>(resultCode);
    }

    public static <T> CommonResult<T> fail(ResultCode resultCode, String msg) {
        return new CommonResult<>(resultCode, msg);
    }

    public static <T> CommonResult<T> status(boolean flag) {
        return (CommonResult<T>)(flag ? success("操作成功") : fail("操作失败"));
    }

    public static <T> CommonResult<T> status(boolean status, String msg) {
        return (CommonResult<T>)(status ? success() : fail(msg));
    }

    public CommonResult() {}
}
